package com.http;

import android.os.AsyncTask;

import com.android.ocean.util.net.ErrorCode;
import com.android.ocean.util.net.ResEntity;
import com.common.method.CommonData;
import com.common.method.ServerPath;
import com.util.UFileUtil;

/**
 * 
 * @ClassName:CatListAsyncTask上传文件
 * @Description:TODO
 * @author zhuw
 * @date 2013-4-24 下午2:25:30
 */
public class UploadFileTask extends AsyncTask<String, Void, ResultString> {
	public static interface CallBack extends NetWorkCallBack {
		/**
		 * 
		 * @Description:TODO
		 * @param result
		 * @return void
		 * @author zhuw
		 * @date 2013-5-6 上午11:43:22
		 */
		public abstract void procUploadFile(String result);
		public abstract void procUploadFileError(String result);

	}

	CallBack callback;

	public UploadFileTask(CallBack callback) {
		super();
		this.callback = callback;
	}

	@Override
	protected ResultString doInBackground(String... params) {
		ResultString result=null;

		try {
			ResEntity re = UFileUtil.uploadFile(ServerPath.IMG_UPLOAD_PATH,params[0], params[1]);
			if(re.code==ResEntity.S_OK){
				/*JSONObject jo=new JSONObject(re.jsonStr);
				StringBuffer sb=new StringBuffer();
				sb.append(jo.opt("thumbnail_src"));
				sb.append("||");
				sb.append(jo.opt("src"));*/
				
				result= new ResultString(CommonData.H_FLAG_SUCCESS, null, re.jsonStr);
			}else{
				result= new ResultString(CommonData.H_FLAG_ERROR, "code:"+re.code+ErrorCode.ERROR_MSG, null);
			}
		} catch (Exception e) {
			result= new ResultString(CommonData.H_FLAG_ERROR, ErrorCode.ERROR_MSG, null);
			e.printStackTrace();

		}
		return result;
	}

	@Override
	protected void onPostExecute(ResultString result) {
		if (result.flag == CommonData.H_FLAG_SUCCESS) {
			callback.procUploadFile(result.result);

		} else {
			callback.procUploadFileError(result.msg);
		}

	}
}
